# Archlive-pkg小组优化
# 感谢WinniChen兄弟对插件集成选择的建议

pkgname=firefox-archlive
pkgver=3.0.11
_ffver=3.0
pkgrel=2
pkgdesc="基于Firefox中文版特别定制，加入了较多常用插件"
url="http://g-fox.cn/index.html"
license=('MPL' 'GPL2' 'LGPL2')
arch=(i686)
provides=('firefox=3.0.11')
conflicts=('firefox' 'firefox-i18n' 'firefox-china')
install="bin32-firefox.install"
depends=('sqlite3' 'nss' 'libxt' 'gtk2' 'desktop-file-utils')
source=(
  firefox.desktop
  firefox-safe.desktop  distribution.ini
  specilink_1.1.xpi #http://blogimg.chinaunix.net/blog/upfile2/090629162709.tar
  firefox-$pkgver.tar.bz2::"http://download.mozilla.org/?product=firefox-$pkgver&os=linux&lang=zh-CN"
  http://g-fox.cn/chinaedition/addons/cpmanager/cpmanager-0.2.1.xpi   #中国版插件管理器，总是自己装新插件！
  http://g-fox.cn/chinaedition/addons/zoompanel/zoompanel-0.2.1.xpi
  http://g-fox.cn/chinaedition/addons/fontsetter/fontsetter-0.2.6.xpi	   #设置字体的插件
  http://g-fox.cn/chinaedition/addons/livemargins/livemargins-0.1.15.xpi
  http://g-fox.cn/chinaedition/addons/quicklaunch/quicklaunch-0.2.8.xpi  #updated
  http://g-fox.cn/chinaedition/addons/tabimprovelite/tabimprovelite-0.2.5.xpi
  cloudberry_tweetfox.xpi::https://addons.mozilla.org/zh-CN/firefox/downloads/file/53953/cloudberry_tweetfox_1.0-1.0.0.10-fx.xpi?confirmed #TweetFox is a Twitter plug-in for Firefox
  #https://addons.mozilla.org/zh-CN/firefox/downloads/latest/9591/addon-9591-latest.xpi #Power twitter
  #https://addons.mozilla.org/zh-CN/firefox/downloads/latest/1122/addon-1122-latest.xpi #Tab Mix Plus
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/7172/addon-7172-latest.jar #macosx_theme
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/2325/addon-2325-latest.xpi #RSS Ticker
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/2254/addon-2254-latest.xpi #UnPlug
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/8642/addon-8642-latest.xpi #汉语多语种词典
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/201/addon-201-latest.xpi #DownThemAll!
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/4925/addon-4925-latest.xpi #AutoPager
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/6076/addon-6076-latest.xpi #Better Gmail 2
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/433/addon-433-latest.xpi #Flashblock
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/1879/addon-1879-latest.xpi #MediaWrap
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/673/addon-673-latest.xpi #InFormEnter
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/220/addon-220-latest.xpi #FlashGot
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/748/addon-748-latest.xpi #Greasemonkey
  http://ftp.ntua.gr/pub/www/mozdev/autoproxy/autoproxy-0.2.1.xpi #AutoProxy 11009
  http://dl.google.com/gears/current/gears-linux-opt.xpi
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/4808/addon-4808-latest.xpi #Google Reader Watcher
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/427/addon-427-latest.xpi #ScrapBook
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/1865/addon-1865-latest.xpi #Adblock Plus
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/6623/addon-6623-latest.xpi #BetterPrivacy
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/4810/addon-4810-latest.xpi #Speed Dial 
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/6777/addon-6777-latest.xpi #IE8 Activities (Accelerators) 
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/2448/platform:2/addon-2448-latest.xpi  #GBookmarks (Google Bookmarks for Firefox) 0.4 
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/1320/addon-1320-latest.xpi #Gmail manager
  https://addons.mozilla.org/zh-CN/firefox/downloads/latest/6113/addon-6113-latest.xpi  #新同文堂
  FireFTP-1.0.4-CN.xpi #https://addons.mozilla.org/zh-CN/firefox/downloads/latest/684/addon-684-latest.xpi #FireFTP英文版
)

md5sums=()

_firefox-xpi-installer() {

  local _dir=${pkgdir}/usr/lib/firefox-${_ffver}/extensions
  mkdir -p "${_dir}/${_xpifile}"
  bsdtar -x -f ${_xpifile} -C "${_dir}/${_xpifile}"
  find "${_dir}/${_xpifile}" -type f -exec chmod 0644 {} \; && \
  _identifier=`awk 'BEGIN{FS="<em:id>"; RS="</"}$NF&&$NF!~/.*>.*/{print $NF}' "${_dir}/${_xpifile}"/install.rdf | head -1`
  if [ "${_identifier}${_xpifile}" = "${_xpifile}" ]; then
       _identifier=`grep "em:id" "${_dir}/${_xpifile}"/install.rdf | head -1 | awk 'BEGIN{FS="\""; RS="\""}$NF&&$NF!~/.*=.*/{print $1}'`
  fi
  #if [ "${_identifier}${_xpifile}" = "${_xpifile}" ]; then
  #     _identifier=`grep "em:id" "${_dir}/${_xpifile}"/install.rdf | head -1 | awk 'BEGIN{FS="{"; RS="}"}$NF&&$NF!~/.*=.*/{print $1}'`
  #fi
  if [ "${_identifier}${_xpifile}" = "${_xpifile}" ]; then
        echo "插件${_xpifile}不支持目前的Firefox版本,请检查其install.rdf文件"
        rm -rf "${_dir}/${_xpifile}"
	  return 0
  else 
     mv "${_dir}/${_xpifile}" "${_dir}/${_identifier}"
     touch "${_dir}/${_identifier}/chrome.manifest"
     return 0
     chmod 777 "${_dir}/${_xpifile}"
  fi
}

build() {
  install -d $pkgdir/usr/lib/firefox-${_ffver} $pkgdir/usr/bin/
  cp -a $srcdir/firefox/* ${pkgdir}/usr/lib/firefox-${_ffver}
  ln -s /usr/lib/firefox-${_ffver}/firefox $pkgdir/usr/bin/firefox

  install -d ${pkgdir}/usr/share/applications/ ${pkgdir}/usr/share/pixmaps/
  install -m644 ${srcdir}/firefox.desktop ${pkgdir}/usr/share/applications/
  install -m644 ${srcdir}/firefox-safe.desktop ${pkgdir}/usr/share/applications/
  install -m644 ${srcdir}/firefox/icons/mozicon128.png ${pkgdir}/usr/share/pixmaps/firefox.png

  install -d ${pkgdir}/usr/lib/firefox-${_ffver}/distribution
  install -m644 ${srcdir}/distribution.ini ${pkgdir}/usr/lib/firefox-${_ffver}/distribution/
  #卸载默认插件，安装另外插件
  rm -rf ${pkgdir}/usr/lib/firefox-${_ffver}/extensions/*
  cd ${srcdir}
  ls *.xpi *.jar > "${srcdir}/xpi"
  cat "${srcdir}/xpi" | while read xpifile; do
    _xpifile=${xpifile}
    _firefox-xpi-installer || return 1
  done   
  chmod 777 ${pkgdir}/usr/lib/firefox-${_ffver}/extensions
  rm -rf $srcdir/*
}
